﻿#include "setup2wind.h"
#include "ui_setup2wind.h"
intListener setup2wind::typechangefl;
extern intListener doorStatus;


setup2wind::setup2wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup2wind)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    typechangefl.set(0);

    infoch = new infoChange();
    connect(&typechangefl, &intListener::valueChanged, this,&setup2wind::exchangetype);
    saNames();
    saTypes();
    saSizes();
    chan1();
    chan2();
    chan3();
    chan4();
    rwtd();
    sjh();
    saConsumableType();
    connect(&doorStatus, &intListener::valueChanged,this,&setup2wind::doorStatusChanged,Qt::QueuedConnection);
    ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->pushButton_9->setEnabled(false);
//    ui->label_7->setVisible(false);
//    ui->consumableType->setVisible(false);
}
void setup2wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}
setup2wind::~setup2wind()
{
    delete ui;
}

void setup2wind::exchangetype(){
    saNames();
    saTypes();
    saSizes();
    chan1();
    chan2();
    chan3();
    chan4();
    saConsumableType();
    rwtd();
    sjh();
}

void setup2wind::showEvent(QShowEvent *event)
{
    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    event->accept();
}

// 样本名
void setup2wind::saNames(){

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=1 ";
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sanames;
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames.append(column1Value);
    }
    query.clear();

    ui->defaultSampleName->clear();
    ui->defaultSampleName->addItems(sanames);

    sql="SELECT Name FROM ItemCode WHERE DicCode=1 and IsDefault=1 ";
    query.exec(sql);

    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    query.clear();

    if(sanames2.length()>0){
        ui->defaultSampleName->setCurrentText(sanames2[0]);
    }
}

// 样本类型
void setup2wind::saTypes()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=2";
    query.exec(sql);

    QStringList satypes;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        satypes.append(column1Value);
    }

    ui->defaultSampleType->clear();
    ui->defaultSampleType->addItems(satypes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=2 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->defaultSampleType->setCurrentText(sanames2[0]);
    }
}
// 样本体系
void setup2wind::saSizes()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=4";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->defaultAmpSystem->clear();
    ui->defaultAmpSystem->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=4 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->defaultAmpSystem->setCurrentText(sanames2[0]);
    }
}
// 通道一
void setup2wind::chan1()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=7";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->FAM->clear();
    ui->FAM->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=7 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->FAM->setCurrentText(sanames2[0]);
    }
}
// 通道二
void setup2wind::chan2()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=8";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->VIC->clear();
    ui->VIC->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=8 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->VIC->setCurrentText(sanames2[0]);
    }
}
// 通道三
void setup2wind::chan3()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=9";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->ROX->clear();
    ui->ROX->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=9 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->ROX->setCurrentText(sanames2[0]);
    }
}
// 通道四
void setup2wind::chan4()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=10";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->CY5->clear();
    ui->CY5->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=10 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->CY5->setCurrentText(sanames2[0]);
    }
}
// 内标
void setup2wind::rwtd()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=11";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->rwtd_cb->clear();
    ui->rwtd_cb->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=11 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->rwtd_cb->setCurrentText(sanames2[0]);
    }else{
        ui->rwtd_cb->addItem(" ");
        ui->rwtd_cb->setCurrentText(" ");
    }
}
// 试剂盒
void setup2wind::sjh(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList sasizes;

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=12";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sasizes.append(column1Value);
    }

    ui->mrsjh_bt->clear();
    ui->mrsjh_bt->addItems(sasizes);

    sql="SELECT Name FROM ItemCode WHERE DicCode=12 and IsDefault=1 ";
    query.exec(sql);
    QStringList sanames2;
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        sanames2.append(column1Value);
    }
    if(sanames2.length()>0){
        ui->mrsjh_bt->setCurrentText(sanames2[0]);
    }else{
        ui->mrsjh_bt->addItem(" ");
        ui->mrsjh_bt->setCurrentText(" ");
    }
}

void setup2wind::saConsumableType(){

}

// 默认信息修改弹窗
void setup2wind::on_infochange_clicked()
{
    infoch->show();

}

// 样本名默认值更改
void setup2wind::on_defaultSampleName_currentIndexChanged(const QString &arg1)
{
    if(namefl!=0){
        QSqlQuery query;
        QString updateSql = "UPDATE ItemCode SET IsDefault = :newValue WHERE DicCode=1";
        query.prepare(updateSql);
        query.bindValue(":newValue", "0"); // 绑定新的值
        if (!query.exec()) {
            qDebug() << "Error:" << query.lastError().text();
        }

        QSqlQuery query2;
        QString updateSql2 = "UPDATE ItemCode SET IsDefault = :newValue WHERE DicCode=1 and Name= :nameValue2";
        query2.prepare(updateSql2);
        query2.bindValue(":newValue", "1"); // 绑定 IsDefault 列的新值
        query2.bindValue(":nameValue2", arg1); // 绑定 Name 列的匹配值
        if (!query2.exec()) {
            qDebug() << "Error:" << query2.lastError().text();
        }
    }else{
        namefl++;
    }

}

// 样本类型默认值更改
void setup2wind::on_defaultSampleType_currentIndexChanged(const QString &arg1)
{
    if(typefl!=0){
        QSqlQuery query;
        QString updateSql = "UPDATE ItemCode SET IsDefault = :newValue WHERE DicCode=2";
        query.prepare(updateSql);
        query.bindValue(":newValue", "0"); // 绑定新的值
        if (!query.exec()) {
            qDebug() << "Error:" << query.lastError().text();
        }

        QSqlQuery query2;
        QString updateSql2 = "UPDATE ItemCode SET IsDefault = :newValue WHERE DicCode=2 and Name= :nameValue2";
        query2.prepare(updateSql2);
        query2.bindValue(":newValue", "1"); // 绑定 IsDefault 列的新值
        query2.bindValue(":nameValue2", arg1); // 绑定 Name 列的匹配值
        if (!query2.exec()) {
            qDebug() << "Error:" << query2.lastError().text();
        }
    }else{
        typefl++;
    }
}

// 样本体系默认值更改
void setup2wind::on_defaultAmpSystem_currentIndexChanged(const QString &arg1)
{
    if(sizefl!=0){
        QSqlQuery query;
        QString updateSql = "UPDATE ItemCode SET IsDefault = :newValue WHERE DicCode=4";
        query.prepare(updateSql);
        query.bindValue(":newValue", "0"); // 绑定新的值
        if (!query.exec()) {
            qDebug() << "Error:" << query.lastError().text();
        }

        QSqlQuery query2;
        QString updateSql2 = "UPDATE ItemCode SET IsDefault = :newValue WHERE DicCode=4 and Name= :nameValue2";
        query2.prepare(updateSql2);
        query2.bindValue(":newValue", "1"); // 绑定 IsDefault 列的新值
        query2.bindValue(":nameValue2", arg1); // 绑定 Name 列的匹配值
        if (!query2.exec()) {
            qDebug() << "Error:" << query2.lastError().text();
        }
    }else{
        sizefl++;
    }
}

void setup2wind::on_changeModel_currentTextChanged(const QString &arg1)
{
    MainWindow::HoleNum=arg1.toInt();
}

void setup2wind::on_SaveBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup2wind::on_returnBtn_clicked()
{
    this->close();
    infoch->close();
    emit changeTotalWindow();
}

void setup2wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}
